lobsterlayui.main(['index', 'layer', 'form', 'element', 'table'], {
    data: {
        booktable: null,
        booktableoptions: {},
        layerindex: 0
    },
    pageload: function (right) {
        console.log(right);
        this.initevent();
        this.loadbook();
    },
    //打开弹层
    openlayer: function (title, elem, successfunction, yesfunction) {
        var self = this;
        var layer = layui.layer;
        var form = layui.form;

        layer.open({
            type: 1
            , zIndex: 1000
            , title: title
            , content: $(elem).html()
            , maxmin: true
            , area: ['680px', '480px']  //宽，高
            , btn: ['保存', '取消']
            , success: function (layero, index) {
                if (successfunction) {
                    successfunction();
                }
            }
            , btn2: function (index, layero) {
                $("#LAY-app-form-reset").trigger('click');
            }
            , cancel: function () {
                $("#LAY-app-form-reset").trigger('click');
            }
            , yes: function (index, layero) {
                var submit = $('#LAY-app-front-submit');
                //监听提交
                form.on('submit(LAY-app-front-submit)', function (data) {
                    var field = data.field; //获取提交的字段
                    var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    console.log(field);
                    self.data.layerindex = index;
                    if (yesfunction) {
                        yesfunction(field);
                    }
                    // self.savebook(field);
                });
                submit.trigger('click');
            }
        });
    },
    //关闭弹层
    closelayer: function () {
        layer.close(this.data.layerindex);
    },
    //初始化事件
    initevent: function () {
        var self = this;
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;

        self.data.booktableoptions = {
            headers: lobsterlayui.GetHeaderData(),
            elem: '#bookTable',
            where: {
                BookName: $("#txtName option:selected").text()  //获取文本值
            },
            height: 'full-130',
            cellMinWidth: 80,
            size: 'sm',
            toolbar: '#toolbar',
            page: true,
            even: true,
            cols: [[
                { width: "70", field: 'Id', title: 'ID', fixed: 'left', hide: true },
                { width: "150", field: 'BookName', title: '书籍名称' },
                { width: "200", field: 'BuyPrice', title: '价格' },
                { width: "200", field: 'BuyDate', title: '购买时间' },
                { width: "90", field: 'Flag', title: '状态标识', align: 'center', templet: '#flagTpl' },
                { width: "235", title: '操作', fixed: 'right', align: 'center', toolbar: '#option' }   //操作script的id                  
            ]],
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.data
                }
            }
        }

        //查找reload,通过条件，查询网格的table重载数据显示到界面
        form.on("submit(btnSearch)", function (data) {
            self.searchbook(data);
        });

        //添加按钮的点击事件
        table.on('toolbar(bookTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    self.addbook();
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(bookTable)', function (obj) {  //tool是工具条事件名，table原始容器的属性
            var data = obj.data;//当前行数据
            var layEvent = obj.event;//获得lay-event的值
            if (obj.event === "edit") {
                self.editbook(data);
            }
            // 启用停用机构
            if (obj.event === "disable" || obj.event === "enable") {

                var confirm_msg = "";
                var status = 0;
                if (data.Flag == 0) {
                    confirm_msg = "确认停用么?";
                    status = 1;
                }
                else {
                    confirm_msg = "确认启用么?";
                    status = 0;
                }

                layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                    function (index) {
                        var Id = data.Id;
                        self.data.layerindex = index;
                        self.deletebook(Id);
                    });
            }
        });

    },
    //查询
    searchbook: function (formdata) {

        this.data.booktable.reload({
            where: {
                BookName: formdata.field.txtName
            },
            page: {
                curr: 1
            }
        });
    },
    //加载书籍列表
    loadbook: function () {
        var self = this;
        var table = layui.table;
        self.data.booktableoptions.url = lobsterlayui.GetLowCodeUrl("tsbook.getdata");
        //表格绑定
        self.data.booktable = table.render(self.data.booktableoptions);
    },
    //添加
    addbook: function () {
        var self = this;
        self.openlayer("新增书籍", "#dialog", function () {


        }, function (field) {
            self.savebook(field);
        });
    },
    //编辑
    editbook: function (row) {
        var self = this;
        var form = layui.form;

        self.openlayer("编辑书籍", "#dialog", function () {
            form.val('formBook', row);
        }, function (field) {
            self.savebook(field);
        });
    },
    //保存书籍
    savebook: function (field) {
        var self = this;
        var layer = layui.layer;

        lobsterlayui.POSTAPI("tsbook.savemodel", field).then(data => {
            layer.msg("保存成功");

            self.data.booktable.reload(); //数据刷新
            self.closelayer(); //关闭弹层
        }).catch(res => {
            layer.msg(res.msg);
            self.closelayer(); //关闭弹层
        });

    },
    //删除书籍
    deletebook: function (Id) {
        var self = this;

        lobsterlayui.GETAPI("tsbook.deletemodel", { Id: Id }).then(data => {
            self.data.booktable.reload();// 数据刷新
            self.closelayer(); //关闭弹层
        });
    }
});